using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Peradigm.Framework.Persistence;

using Liberty.Domain.ServiceMemberManagement;
using Liberty.Persistence.GrantManagement;
using Liberty.Persistence.Kernel;

namespace Liberty.Persistence.ServiceMemberManagement
{
    public class RankBroker : AbstractBroker
    {
        #region Constants

        private const string
            RANK_WITH = "ServiceMemberManagement.RankWith",
            RANKS_FOR_MILITARY_BRANCH = "ServiceMemberManagement.RanksForMilitaryBranch",
            SP_GET_ALL_MILITARY_BRANCHES = "ServiceMemberManagement.GetAllTypesOfMilitaryBranch";

        private const string FIELD_PREFIX = "@";

        private const string
            FLD_OID = "OID",
            FLD_VERSION = "Version",
            FLD_BRANCH_ID = "BranchOfServiceID",
            FLD_RANK_STRUCTURE = "RankStructure",
            FLD_ABBREVIATION = "Abbreviation",
            FLD_PAY_GRADE = "PayGrade",
            FLD_RANK = "Rank";

        #endregion

        #region Event Handlers
        #endregion

        #region Methods
        public List<Rank> RanksFor( Branch branchOfService )
        {
            List<Rank> ranks = new List<Rank>();
            SafeReader reader = null;
            SqlCommand cmd = null;

            try
            {
                cmd = this.CommandFor( RANKS_FOR_MILITARY_BRANCH );

                cmd.Parameters.Add( new SqlParameter( FIELD_PREFIX + FLD_BRANCH_ID, branchOfService.Oid ) );

                reader = this.ExecuteReader( cmd );
                while( reader.Read() )
                {
                    Rank myRank = this.RankFrom( reader, branchOfService );
                    ranks.Add( myRank );
                }
            }
            finally
            {
                this.Close( cmd );
                this.Close( reader );
            }

            return ranks;
        }

        public Rank RankWith( long rankID )
        {
            SqlCommand cmd = null;
            SafeReader reader = null;
            Rank myRank = null;

            try
            {
                cmd = this.CommandFor( RANK_WITH );

                cmd.Parameters.Add( new SqlParameter( FIELD_PREFIX + FLD_OID, rankID ) );

                reader = this.ExecuteReader( cmd );
                if( reader.Read() )
                {
                    myRank = this.RankFrom( reader, null );
                }
            }
            catch( Exception )
            {
                throw;
            }
            finally
            {
                this.Close( cmd );
                this.Close( reader );
            }

            return myRank;
        }
        #endregion

        #region Properties
        #endregion

        #region Private Methods

        internal Rank RankFrom( SafeReader reader, Branch branchOfService )
        {
            long oid = reader.GetInt64( FLD_OID );
            byte[] version = reader.GetRowVersion( FLD_VERSION );
            string abbreviation = reader.GetString( FLD_ABBREVIATION );
            string payGrade = reader.GetString( FLD_PAY_GRADE );
            string rank = reader.GetString( FLD_RANK );
            string rankStructure = reader.GetString( FLD_RANK_STRUCTURE );
            if( branchOfService == null )
            {
                long branchOID = reader.GetInt64( FLD_BRANCH_ID );
                branchOfService = new ReferenceValueBroker<Branch>( this.ConnectionString, SP_GET_ALL_MILITARY_BRANCHES ).ValueWith( branchOID );
            }
            Rank myRank = new Rank( oid, version, abbreviation, branchOfService, payGrade, rank, rankStructure );

            return myRank;
        }

        #endregion

        #region Private Properties
        #endregion

        #region Construction and Finalization

        public RankBroker( SqlTransaction txn )
            : base( txn )
        {
        }

        public RankBroker( string cxnString )
            : base( cxnString )
        {
        }

        #endregion

        #region Public Methods

        #endregion

        #region Data Elements
        #endregion
    }
}
